package com.industrial.premu.service.impl;

import java.util.Date;
import java.util.List;

import com.industrial.common.core.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.industrial.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.industrial.premu.domain.PreMuIndustryValues;
import com.industrial.premu.mapper.PreMuIndustryCoefficientMapper;
import com.industrial.premu.domain.PreMuIndustryCoefficient;
import com.industrial.premu.service.IPreMuIndustryCoefficientService;

/**
 * 行业系数Service业务层处理
 * 
 * @author ZhouS
 * @date 2024-01-23
 */
@Service
public class PreMuIndustryCoefficientServiceImpl implements IPreMuIndustryCoefficientService 
{
    @Autowired
    private PreMuIndustryCoefficientMapper preMuIndustryCoefficientMapper;

    /**
     * 查询行业系数
     * 
     * @param baseId 行业系数主键
     * @return 行业系数
     */
    @Override
    public PreMuIndustryCoefficient selectPreMuIndustryCoefficientByBaseId(String baseId)
    {
        return preMuIndustryCoefficientMapper.selectPreMuIndustryCoefficientByBaseId(baseId);
    }

    /**
     * 查询行业系数列表
     * 
     * @param preMuIndustryCoefficient 行业系数
     * @return 行业系数
     */
    @Override
    public List<PreMuIndustryCoefficient> selectPreMuIndustryCoefficientList(PreMuIndustryCoefficient preMuIndustryCoefficient)
    {
        return preMuIndustryCoefficientMapper.selectPreMuIndustryCoefficientList(preMuIndustryCoefficient);
    }

    /**
     * 新增行业系数
     * 
     * @param preMuIndustryCoefficient 行业系数
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPreMuIndustryCoefficient(PreMuIndustryCoefficient preMuIndustryCoefficient)
    {
        preMuIndustryCoefficient.setBaseId(IdUtils.simpleUUID());
        preMuIndustryCoefficient.setBaseCreateTime(new Date());
        int rows = preMuIndustryCoefficientMapper.insertPreMuIndustryCoefficient(preMuIndustryCoefficient);
        insertPreMuIndustryValues(preMuIndustryCoefficient);
        return rows;
    }

    /**
     * 修改行业系数
     * 
     * @param preMuIndustryCoefficient 行业系数
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePreMuIndustryCoefficient(PreMuIndustryCoefficient preMuIndustryCoefficient)
    {
        preMuIndustryCoefficientMapper.deletePreMuIndustryValuesByIndustryId(preMuIndustryCoefficient.getBaseId());
        insertPreMuIndustryValues(preMuIndustryCoefficient);
        return preMuIndustryCoefficientMapper.updatePreMuIndustryCoefficient(preMuIndustryCoefficient);
    }

    /**
     * 批量删除行业系数
     * 
     * @param baseIds 需要删除的行业系数主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePreMuIndustryCoefficientByBaseIds(String[] baseIds)
    {
        preMuIndustryCoefficientMapper.deletePreMuIndustryValuesByIndustryIds(baseIds);
        return preMuIndustryCoefficientMapper.deletePreMuIndustryCoefficientByBaseIds(baseIds);
    }

    /**
     * 删除行业系数信息
     * 
     * @param baseId 行业系数主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePreMuIndustryCoefficientByBaseId(String baseId)
    {
        preMuIndustryCoefficientMapper.deletePreMuIndustryValuesByIndustryId(baseId);
        return preMuIndustryCoefficientMapper.deletePreMuIndustryCoefficientByBaseId(baseId);
    }

    /**
     * 新增行业系数值信息
     * 
     * @param preMuIndustryCoefficient 行业系数对象
     */
    public void insertPreMuIndustryValues(PreMuIndustryCoefficient preMuIndustryCoefficient)
    {
        List<PreMuIndustryValues> preMuIndustryValuesList = preMuIndustryCoefficient.getPreMuIndustryValuesList();
        String baseId = preMuIndustryCoefficient.getBaseId();
        if (StringUtils.isNotNull(preMuIndustryValuesList))
        {
            List<PreMuIndustryValues> list = new ArrayList<PreMuIndustryValues>();
            for (PreMuIndustryValues preMuIndustryValues : preMuIndustryValuesList)
            {
                preMuIndustryValues.setIndustryId(baseId);
                preMuIndustryValues.setBaseId(IdUtils.simpleUUID());
                list.add(preMuIndustryValues);
            }
            if (list.size() > 0)
            {
                preMuIndustryCoefficientMapper.batchPreMuIndustryValues(list);
            }
        }
    }
}
